package com.ruoyi.health.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.health.mapper.NoticeCategoryMapper;
import com.ruoyi.health.domain.NoticeCategory;
import com.ruoyi.health.service.INoticeCategoryService;

/**
 * 资讯分类Service业务层处理
 * 
 * @author dalian
 * @date 2023-08-05
 */
@Service
public class NoticeCategoryServiceImpl implements INoticeCategoryService 
{
    @Autowired
    private NoticeCategoryMapper noticeCategoryMapper;

    /**
     * 查询资讯分类
     * 
     * @param id 资讯分类主键
     * @return 资讯分类
     */
    @Override
    public NoticeCategory selectNoticeCategoryById(Long id) {
        return noticeCategoryMapper.selectNoticeCategoryById(id);
    }

    /**
     * 查询资讯分类列表
     * 
     * @param noticeCategory 资讯分类
     * @return 资讯分类
     */
    @Override
    public List<NoticeCategory> selectNoticeCategoryList(NoticeCategory noticeCategory) {
        return noticeCategoryMapper.selectNoticeCategoryList(noticeCategory);
    }

    /**
     * 新增资讯分类
     * 
     * @param noticeCategory 资讯分类
     * @return 结果
     */
    @Override
    public int insertNoticeCategory(NoticeCategory noticeCategory) {
        noticeCategory.setCreateTime(DateUtils.getNowDate());
        //判断是几级分类（只有两级）
        if(noticeCategory.getParentId()==0 ){
            noticeCategory.setCateLevel(1L);    //一级
        }else {
            noticeCategory.setCateLevel(2L);    //二级
        }
        return noticeCategoryMapper.insertNoticeCategory(noticeCategory);
    }

    /**
     * 修改资讯分类
     * 
     * @param noticeCategory 资讯分类
     * @return 结果
     */
    @Override
    public int updateNoticeCategory(NoticeCategory noticeCategory) {
        //判断是几级分类（只有两级）
        if(noticeCategory.getParentId()==0 ){
            noticeCategory.setCateLevel(1L);    //一级
        }else {
            noticeCategory.setCateLevel(2L);    //二级
        }
        return noticeCategoryMapper.updateNoticeCategory(noticeCategory);
    }

    /**
     * 删除资讯分类信息
     * 
     * @param id 资讯分类主键
     * @return 结果
     */
    @Override
    public int deleteNoticeCategoryById(Long id) {
        //删除之前，先检查该类别下面有没有子类型
        int subNoticeCategoryCount = noticeCategoryMapper.selectCountSubNoticeCategoryByParentId(id);
        if(subNoticeCategoryCount>0){
            throw new ServiceException("当前资讯分类下有子类型，无法删除！");
        }
        return noticeCategoryMapper.deleteNoticeCategoryById(id);
    }
}
